package 领扣算法.力扣计划.算法基础.填充每个节点的下一个右侧节点指针II;

import java.util.LinkedList;

public class Main {
    public Node connect(Node root) {
        LinkedList<Node> list=new LinkedList<>();
        list.add(root);
        while (!list.isEmpty()){
            Node node=list.pollFirst();
            Node middle=node;
           while (!list.isEmpty())
           {
               node.next=list.pollFirst();
               node=node.next;

           }
            while (middle!=null){
                if (middle.left!=null)
                    list.addLast(middle.left);
                if (middle.right!=null)
                    list.addLast(middle.right);
                middle=middle.next;
            }
        }
        return root;

    }

    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    }
}